<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务，帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件，不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types = 1);

namespace app\api\model\jia;

use app\api\service\User as UserService;
use app\common\model\jia\Feedback as FeedbackModel;
use app\common\model\jia\FeedbackImage as CommentImage;
use app\store\service\wxapp\Message as MessageService;
use think\facade\Db;

/**
 * 商家门店模型
 * Class Shop
 * @package app\store\model\store
 */
class Feedback extends FeedbackModel
{
    /**
     * 隐藏字段
     * @var array
     */
    protected $hidden = [
        'is_delete',
        'store_id',
        'create_time',
        'update_time',
    ];

/**
 * 根据已完成订单商品 添加评价
 * @param OrderModel $order
 * @param $goodsList
 * @param array $data
 * @return boolean
 * @throws BaseException
 */
    public function increased(array $data): bool
    {

        return $this->transaction(function () use ($data) {
            // 记录评价内容
            $data['store_id'] = self::$storeId;
            $data['user_id']  = UserService::getCurrentLoginUserId();

            $this->save($data);
            if (isset($data['uploaded'])) {
                // 记录评价图片`
                $this->saveAllImages($this['id'], $data);
            }
            $this->sendMessage($data);

            return true;
        });
    }
    private function sendMessage($order)
    {
        //康护主管
        $clerks = Db::name('store_shop_manage')
            ->where('status', 1)->where('type', 4)
          
            ->where('is_delete', 0)
            ->group('oauth_id')->field('oauth_id')
            ->select()->toArray();

        foreach ($clerks as $key => $value) {

            $opend_id = $value['oauth_id'];

            if ($opend_id) {
                // 执行发送

                $MessageService         = new MessageService;
                $content['template_id'] = '7yD6xwh2o5cdKNXxZYASqAzkGYProWGi2WGaorgKD8g';
                $content['page']        = '';

                $content['data']['thing5']        = $order['name'];
                $content['data']['phone_number2'] = $order['phone'];
                $content['data']['time6']         = Date('Y-m-d H:i');

                //暂时注释
                $MessageService->send($opend_id, $content);
            }
        }
    }

    /**
     * 记录评价图片
     * @param $commentList
     * @param $formData
     * @return void
     */
    private function saveAllImages($comment_id, $formData): void
    {
        // 生成评价图片数据
        $imageData = [];
        foreach ($formData['uploaded'] as $imageId) {
            $imageData[] = [
                'feedback_id' => $comment_id,
                'image_id'    => $imageId,
                'store_id'    => self::$storeId,
            ];
        }

        $model = new CommentImage;
        ! empty($imageData) && $model->addAll($imageData) !== false;
    }
}
